package com.hghivln.erpwarehouse.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hghivln.erpwarehouse.business.entity.*;
import com.hghivln.erpwarehouse.business.entity.Sales;
import com.hghivln.erpwarehouse.business.mapper.SalesMapper;
import com.hghivln.erpwarehouse.business.service.CustomerService;
import com.hghivln.erpwarehouse.business.service.GoodsService;
import com.hghivln.erpwarehouse.business.service.SalesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.erpwarehouse.business.vo.SalesVo;
import com.hghivln.erpwarehouse.system.common.lang.ActiverUser;
import com.hghivln.erpwarehouse.system.common.lang.DataGridlView;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-07-13
 */
@Service
public class SalesServiceImpl extends ServiceImpl<SalesMapper, Sales> implements SalesService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CustomerService customerService;

    @Override
    public DataGridlView QueryAllSales(SalesVo salesVo) {
        Page<Sales> page = new Page<>(salesVo.getPage(),salesVo.getLimit());
        QueryWrapper<Sales> qw = new QueryWrapper<>();
        qw.eq(!StringUtils.isEmpty(salesVo.getGoodsid()),"goodsid",salesVo.getGoodsid());
        qw.eq(!StringUtils.isEmpty(salesVo.getCustomerid()),"customerid",salesVo.getCustomerid());
        qw.ge(salesVo.getStartDate()!=null,"salestime",salesVo.getStartDate());
        qw.le(salesVo.getEndDate()!=null,"salestime",salesVo.getEndDate());
        getBaseMapper().selectPage(page, qw);
        List<Sales> records = page.getRecords();
        for (Sales sales : records) {
            if (sales.getGoodsid() != null){
                Goods goods = goodsService.getById(sales.getGoodsid());
                sales.setSize(goods.getSize());
                sales.setGoodsname(goods.getGoodsname());
            }
            if (sales.getCustomerid() != null) {
                Customer  customer = customerService.getById(sales.getCustomerid());
                sales.setCustomername(customer.getCustomername());
            }
        }
        return new DataGridlView(page.getTotal(),records);
    }

    @Override
    public Sales saveSales(Sales sales) {
        Subject subject = SecurityUtils.getSubject();
        ActiverUser activerUser = (ActiverUser) subject.getPrincipal();
        sales.setOperateperson(activerUser.getUser().getName());
        sales.setSalestime(new Date());
        getBaseMapper().insert(sales);

        Goods goods = goodsService.getById(sales.getGoodsid());
        goods.setNumber(goods.getNumber()-sales.getNumber());
        goodsService.updateGoods(goods);

        return sales;
    }

    @Override
    public boolean updateById(Sales entity) {
        return super.updateById(entity);
    }
}
